package com.woniuxy.produce.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.common.dto.SkuDto;
import com.woniuxy.produce.pojo.FinanceSku;
import com.woniuxy.produce.service.FinanceSkuService;
import com.woniuxy.produce.mapper.FinanceSkuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author 76169
* @description 针对表【finance_sku】的数据库操作Service实现
* @createDate 2024-12-17 19:01:14
*/
@Service
public class FinanceSkuServiceImpl extends ServiceImpl<FinanceSkuMapper, FinanceSku>
    implements FinanceSkuService{

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public List<SkuDto> queryAllCartSku() {
        Map<Object, Object> entries = redisTemplate.opsForHash().entries("CART:1");
        ArrayList<SkuDto> skuDtos = new ArrayList<>();
        for (Map.Entry<Object, Object> entry : entries.entrySet()) {
            SkuDto skuDto1 = JSONObject.parseObject(entry.getValue().toString(), SkuDto.class);
            skuDto1.setId(Long.parseLong(entry.getKey().toString()));
            skuDtos.add(skuDto1);
        }
        return skuDtos;
    }

    @Override
    public Boolean updateSku(Long skuId) {
        return lambdaUpdate()
                .eq(FinanceSku::getId,skuId)
                .setSql("finance_sku_stock=finance_sku_stock-1")
                .update();
    }

    @Override
    public void rollbackSku(String skuId) {
        lambdaUpdate().eq(FinanceSku::getId,skuId)
                .setSql("finance_sku_stock=finance_sku_stock+1")
                .update();
    }


}




